package com.iyao_app.im.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iyao_app.R;
import com.iyao_app.base.BaseActivityWithHead;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.util.Contants;
import com.iyao_app.util.DbUtil;
import com.iyao_app.util.ResourcesUtils;
import com.iyao_app.view.LetterSpacingTextView;
import com.iyaowoqu.base_library.constant.RegexConstants;
import com.iyaowoqu.base_library.util.KeyboardUtils;
import com.iyaowoqu.base_library.util.RegexUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class ModificationPasswordActivity extends BaseActivityWithHead {
    private static final long MAX_COUNT_TIME = 60;

    @BindView(R.id.et_login_account)
    EditText mEtLoginAccount;
    @BindView(R.id.lin_login)
    LinearLayout mLinLogin;
    @BindView(R.id.et_login_password)
    EditText mEtLoginPassword;
    @BindView(R.id.lin_password)
    LinearLayout mLinPassword;
    @BindView(R.id.et_message)
    EditText mEtMessage;
    @BindView(R.id.lin_message)
    LinearLayout mLinMessage;
    @BindView(R.id.tv_fail)
    TextView mTvFail;
    @BindView(R.id.tv_register)
    LetterSpacingTextView mTvRegister;
    @BindView(R.id.rl_root1)
    RelativeLayout mRlRoot1;
    @BindView(R.id.rl_root)
    RelativeLayout mRlRoot;

   // private Bitmap bm;
    private boolean isSend = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modification_password);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        ButterKnife.bind(this);
        initData();
      //  bm = BitmapFactory.decodeResource(this.getResources(), R.mipmap.bg_ditu_denglu);
       // BitmapDrawable bd = new BitmapDrawable(this.getResources(), bm);
      //  mRlRoot.setBackgroundDrawable(bd);
    }

    private void initData() {

        SpannableString s = new SpannableString(ResourcesUtils.getString(R.string.hint_password));
        AbsoluteSizeSpan textSize = new AbsoluteSizeSpan(11, true);
        s.setSpan(textSize, 0, s.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mEtLoginPassword.setHint(s);

        mEtLoginAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mTvFail.getVisibility() == View.VISIBLE) {
                    mTvFail.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEtLoginPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mTvFail.getVisibility() == View.VISIBLE) {
                    mTvFail.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEtMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (mTvFail.getVisibility() == View.VISIBLE) {
                    mTvFail.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEtMessage.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    KeyboardUtils.hideSoftInput(mEtMessage);
                }

                return false;
            }
        });
    }

    @OnClick({R.id.tv_register, R.id.rl_root1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_register:
                check();
                break;

            case R.id.rl_root1:
                KeyboardUtils.hideSoftInput(this);
                break;
        }
    }


    private void check() {
//|| !DbUtil.getUser().password.equals(mEtLoginAccount.getText().toString().trim())
        String str = null;
        if (TextUtils.isEmpty(mEtLoginAccount.getText().toString().trim()) ) {
            str = ResourcesUtils.getString(R.string.password_fail_);
            Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
            mTvFail.setVisibility(View.VISIBLE);
            mTvFail.startAnimation(shake);
            mTvFail.setText(str);
            return;
        }
        if (TextUtils.isEmpty(mEtLoginPassword.getText().toString().trim()) || mEtLoginPassword.getText().toString().trim().length() < 8 || RegexUtils.isMatch(RegexConstants.ALL_ABC, mEtLoginPassword.getText().toString().trim()) || RegexUtils.isMatch(RegexConstants.REGEX_INTEGER, mEtLoginPassword.getText().toString().trim())) {
            str = ResourcesUtils.getString(R.string.password_fail);
            Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
            mTvFail.setVisibility(View.VISIBLE);
            mTvFail.startAnimation(shake);
            mTvFail.setText(str);
            return;
        }
        if (TextUtils.isEmpty(mEtMessage.getText().toString().trim()) || !mEtLoginPassword.getText().toString().trim().equals(mEtMessage.getText().toString().trim()) ) {
            str = ResourcesUtils.getString(R.string.password_no);
            Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
            mTvFail.setVisibility(View.VISIBLE);
            mTvFail.startAnimation(shake);
            mTvFail.setText(str);
            return;
        }
        showDialog();

        NetUtils.getInstance().getUserUpdatepassword(DbUtil.getUser().id,mEtLoginAccount.getText().toString().trim(),mEtLoginPassword.getText().toString().trim(), new NetCallBack() {
                    @Override
                    public void onSuccess(final String message, final int status, String response, final ResultModel model) {
                        closeDialog();
                        if (status == Contants.SUCCESS_CODE) {
                            //todo 成功之后的东西
                            finish();
                        } else {

                        }
                    }

                    @Override
                    public void onFail(String message, int status) {

                    }
                }, null);

    }

}
